package com.robotserver.orders.service.impl;

import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.entity.PddOrders;
import com.robotserver.orders.mapper.PddOrdersMapper;
import com.robotserver.orders.mapper.SysSettingMapper;
import com.robotserver.orders.param.PddOldOrderSyncParam;
import com.robotserver.orders.param.PddOrderPullByOrderSnParam;
import com.robotserver.orders.param.PddOrderSyncParam;
import com.robotserver.orders.param.biz.PddOrderPullParam;
import com.robotserver.orders.service.PddOrderService;
import com.robotserver.orders.service.PddOrderSyncService;
import com.robotserver.orders.vo.OldSyncOrderVO;
import com.robotserver.orders.vo.PddConfig;
import com.robotserver.orders.vo.PddOrderTimeVO;
import com.robotserver.orders.vo.biz.PageVO;
import com.robotserver.orders.vo.biz.PddOrderVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Auther: jianghan
 * @Date: 2019/11/14 09:26
 * @Description:
 */
@Service
public class PddOrderServiceImpl implements PddOrderService {

    private static Logger logger = LoggerFactory.getLogger(PddOrderServiceImpl.class);

    @Autowired
    private PddOrderSyncService pddOrderSyncService;
    @Autowired
    private SysSettingMapper sysSettingMapper;
    @Autowired
    private PddOrdersMapper pddOrdersMapper;

    @Override
    public void syncOrder(PddOrderSyncParam pddOrderSyncParam) {
        List<PddOrderTimeVO> pddOrderTimeVOS = DateUtil.getPddOrderTimeScope(pddOrderSyncParam.getBegin_time(),pddOrderSyncParam.getEnd_time());
        String pdd_client_id = sysSettingMapper.getByKey("pdd_appkey");
        String pdd_client_secret = sysSettingMapper.getByKey("pdd_appsecret");
        PddConfig pddConfig = new PddConfig();
        pddConfig.setPdd_client_id(pdd_client_id);
        pddConfig.setPdd_client_secret(pdd_client_secret);
        logger.info("---------拼多多通过工具商AppKey拉取订单开始------开始------");
        pddOrderSyncService.threadSyncOrder(pddOrderTimeVOS,pddConfig);
    }

    @Override
    public PageVO getOrderList(PddOrderPullParam param) {
        List<PddOrders> list = pddOrdersMapper.getOrderList(param);
        PddOrderVO pddOrderVO = new PddOrderVO();
        pddOrderVO.setRecords(list);
        return pddOrderVO;
    }

    @Override
    public OldSyncOrderVO oldSyncOrder(PddOldOrderSyncParam pddOldOrderSyncParam) {
        OldSyncOrderVO oldSyncOrderVO = new OldSyncOrderVO();
        if (StringUtils.isNotBlank(pddOldOrderSyncParam.getPidStr())){
            pddOldOrderSyncParam.setPids(pddOldOrderSyncParam.getPidStr().split(","));
        }
        int count = pddOrdersMapper.getOldSyncCount(pddOldOrderSyncParam);
        List<Map> list = pddOrdersMapper.getOldSyncOrder(pddOldOrderSyncParam);
        oldSyncOrderVO.setTotal(count);
        oldSyncOrderVO.setList(list);
        return oldSyncOrderVO;
    }

    @Override
    public PddOrders getByOrderId(String orderId) {
        return pddOrdersMapper.getByOrderId(orderId);
    }

    @Override
    public void pullByOrderSn(PddOrderPullByOrderSnParam pddOrderPullByOrderSnParam){
        String pdd_client_id = sysSettingMapper.getByKey("pdd_appkey");
        String pdd_client_secret = sysSettingMapper.getByKey("pdd_appsecret");
        PddConfig pddConfig = new PddConfig();
        pddConfig.setPdd_client_id(pdd_client_id);
        pddConfig.setPdd_client_secret(pdd_client_secret);

        pddOrderSyncService.pullByOrderSn(pddOrderPullByOrderSnParam, pddConfig);

    }
}
